package co.edu.unad.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import co.edu.unad.entities.Rol;
import co.edu.unad.entities.Rolfuncionalidad;
import co.edu.unad.entities.Usuario;

@Repository
public class RolDAO {

	@Autowired
    private SessionFactory sessionFactory;
	
	/**
     * Get Hibernate Session Factory
     *
     * @return SessionFactory - Hibernate Session Factory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * Set Hibernate Session Factory
     *
     * @param sessionFactory SessionFactory - Hibernate Session Factory
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    /**
     * Get customer List
     *
     * @return List - customer list
     */

    public List<Rol> getRoles() {
    	
    	String query = "SELECT r FROM Rol r order by r.idRol";
    	
        List list = getSessionFactory().getCurrentSession().createQuery(query).list();
        return list;
        
    }
    
    public List<Rolfuncionalidad> buscarFuncionalidadPorIdRol(Integer idRol){
    	
    	final Session session = sessionFactory.getCurrentSession();
        final Criteria criteria = session.createCriteria(Rolfuncionalidad.class);

        criteria.add(Restrictions.eq("rolfuncionalidadPK.idRol", idRol));
    	
        List<Rolfuncionalidad> list = criteria.list();
        return list;
    }
	
}
